package com.brayan.melinxa.gui.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;
import java.awt.SystemColor;

public class DestinationPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JFileChooser dirChooser = new JFileChooser("/");
	private JTextField textFieldDirectory;

	public DestinationPanel() {
		setBackground(Color.WHITE);

		dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.WHITE);
		add(panel_2, BorderLayout.NORTH);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));
		
		JPanel panel_Top = new JPanel();
		panel_Top.setBackground(Color.WHITE);
		panel_2.add(panel_Top);
		
		Component verticalStrut = Box.createVerticalStrut(20);
		panel_Top.add(verticalStrut);

		JPanel panel_Content = new JPanel();
		panel_2.add(panel_Content);
		panel_Content.setBackground(Color.WHITE);
		panel_Content.setAlignmentY(Component.TOP_ALIGNMENT);
		GridBagLayout gbl_panel_Content = new GridBagLayout();
		gbl_panel_Content.columnWidths = new int[] { 0, 220, 0, 0 };
		gbl_panel_Content.rowHeights = new int[] { 23, 0, 0, 0, 0, 0 };
		gbl_panel_Content.columnWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
		gbl_panel_Content.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		panel_Content.setLayout(gbl_panel_Content);

		Component horizontalStrut = Box.createHorizontalStrut(20);
		GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
		gbc_horizontalStrut.insets = new Insets(0, 0, 5, 5);
		gbc_horizontalStrut.gridx = 0;
		gbc_horizontalStrut.gridy = 0;
		panel_Content.add(horizontalStrut, gbc_horizontalStrut);

		JLabel labelSelectDirectory = new JLabel("Please select the destination directory.");
		labelSelectDirectory.setAlignmentY(Component.TOP_ALIGNMENT);
		GridBagConstraints gbc_labelSelectDirectory = new GridBagConstraints();
		gbc_labelSelectDirectory.anchor = GridBagConstraints.WEST;
		gbc_labelSelectDirectory.insets = new Insets(0, 0, 5, 5);
		gbc_labelSelectDirectory.gridx = 1;
		gbc_labelSelectDirectory.gridy = 0;
		panel_Content.add(labelSelectDirectory, gbc_labelSelectDirectory);

		Component horizontalStrut_1 = Box.createHorizontalStrut(20);
		GridBagConstraints gbc_horizontalStrut_1 = new GridBagConstraints();
		gbc_horizontalStrut_1.insets = new Insets(0, 0, 5, 0);
		gbc_horizontalStrut_1.gridx = 2;
		gbc_horizontalStrut_1.gridy = 0;
		panel_Content.add(horizontalStrut_1, gbc_horizontalStrut_1);

		textFieldDirectory = new JTextField();
		GridBagConstraints gbc_textFieldDirectory = new GridBagConstraints();
		gbc_textFieldDirectory.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldDirectory.anchor = GridBagConstraints.WEST;
		gbc_textFieldDirectory.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldDirectory.gridx = 1;
		gbc_textFieldDirectory.gridy = 1;
		panel_Content.add(textFieldDirectory, gbc_textFieldDirectory);
		textFieldDirectory.setHorizontalAlignment(SwingConstants.LEFT);
		textFieldDirectory.setAlignmentY(Component.TOP_ALIGNMENT);
		textFieldDirectory.setColumns(10);
		
				JButton btnSelectDirectory = new JButton("select destination directory");
				GridBagConstraints gbc_btnSelectDirectory = new GridBagConstraints();
				gbc_btnSelectDirectory.insets = new Insets(0, 0, 5, 5);
				gbc_btnSelectDirectory.anchor = GridBagConstraints.NORTHWEST;
				gbc_btnSelectDirectory.gridx = 1;
				gbc_btnSelectDirectory.gridy = 3;
				panel_Content.add(btnSelectDirectory, gbc_btnSelectDirectory);
				
				Component verticalStrut_1 = Box.createVerticalStrut(20);
				GridBagConstraints gbc_verticalStrut_1 = new GridBagConstraints();
				gbc_verticalStrut_1.insets = new Insets(0, 0, 0, 5);
				gbc_verticalStrut_1.gridx = 1;
				gbc_verticalStrut_1.gridy = 4;
				panel_Content.add(verticalStrut_1, gbc_verticalStrut_1);
		
				btnSelectDirectory.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						openFileSelector();
					}
				});

		JPanel panel_Description = new JPanel();
		panel_Description.setBackground(Color.WHITE);
		add(panel_Description, BorderLayout.SOUTH);
		panel_Description.setLayout(new BoxLayout(panel_Description, BoxLayout.Y_AXIS));
		
				JSeparator separator = new JSeparator();
				panel_Description.add(separator);
		
		JPanel panel_1 = new JPanel();
		panel_Description.add(panel_1);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
				
				JPanel panel_Graph = new JPanel();
				panel_1.add(panel_Graph);
				panel_Graph.setBackground(SystemColor.textHighlight);
		
		JPanel panel_Detail = new JPanel();
		panel_1.add(panel_Detail);
		panel_Detail.setBackground(Color.WHITE);
		GridBagLayout gbl_panel_Detail = new GridBagLayout();
		gbl_panel_Detail.columnWidths = new int[]{0, 0, 0, 0};
		gbl_panel_Detail.rowHeights = new int[]{0, 0, 0, 0};
		gbl_panel_Detail.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_panel_Detail.rowWeights = new double[]{0.0, 1.0, 1.0, Double.MIN_VALUE};
		panel_Detail.setLayout(gbl_panel_Detail);
		
		JSeparator separator_1 = new JSeparator();
		GridBagConstraints gbc_separator_1 = new GridBagConstraints();
		gbc_separator_1.insets = new Insets(0, 0, 5, 5);
		gbc_separator_1.gridx = 1;
		gbc_separator_1.gridy = 0;
		panel_Detail.add(separator_1, gbc_separator_1);
		
		Component horizontalStrut_2 = Box.createHorizontalStrut(20);
		GridBagConstraints gbc_horizontalStrut_2 = new GridBagConstraints();
		gbc_horizontalStrut_2.insets = new Insets(0, 0, 5, 5);
		gbc_horizontalStrut_2.gridx = 0;
		gbc_horizontalStrut_2.gridy = 1;
		panel_Detail.add(horizontalStrut_2, gbc_horizontalStrut_2);
		
		JTextPane textPaneTitle = new JTextPane();
		textPaneTitle.setContentType("text/html");
		textPaneTitle.setEditable(false);
		textPaneTitle.setText("<b>Destination Directory</b>");
		GridBagConstraints gbc_textPaneTitle = new GridBagConstraints();
		gbc_textPaneTitle.anchor = GridBagConstraints.WEST;
		gbc_textPaneTitle.insets = new Insets(0, 0, 5, 5);
		gbc_textPaneTitle.fill = GridBagConstraints.HORIZONTAL;
		gbc_textPaneTitle.gridx = 1;
		gbc_textPaneTitle.gridy = 1;
		panel_Detail.add(textPaneTitle, gbc_textPaneTitle);
		
		Component horizontalStrut_3 = Box.createHorizontalStrut(20);
		GridBagConstraints gbc_horizontalStrut_3 = new GridBagConstraints();
		gbc_horizontalStrut_3.insets = new Insets(0, 0, 5, 0);
		gbc_horizontalStrut_3.gridx = 2;
		gbc_horizontalStrut_3.gridy = 1;
		panel_Detail.add(horizontalStrut_3, gbc_horizontalStrut_3);
		
		JTextPane txtpnDescription = new JTextPane();
		txtpnDescription.setText("This directory used to store an instantial updated shadow copy of your previously selected source directory.");
		GridBagConstraints gbc_txtpnDescription = new GridBagConstraints();
		gbc_txtpnDescription.insets = new Insets(0, 0, 0, 5);
		gbc_txtpnDescription.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtpnDescription.gridx = 1;
		gbc_txtpnDescription.gridy = 2;
		panel_Detail.add(txtpnDescription, gbc_txtpnDescription);
		this.repaint();
	}

	private void openFileSelector() {
		// dirChooser.showDialog(this, "select directory");

		if (dirChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
			String path = dirChooser.getCurrentDirectory().getPath();
			try {
				textFieldDirectory.setText(dirChooser.getSelectedFile().getCanonicalPath());
			} catch (Exception ex) {
				System.out.println("Failed to evaluate canonical Path of the selected directory: "
						+ path);
			}
		} else {
			System.out.println("No Selection ");
		}
	}

}
